/**
 * 
 */
package org.jenkon.io.filter;

import java.io.File;
import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * {@link FileFilter} implementation to check of match regular expression of filename.
 * 
 * @author Jens Konopka
 * 
 */
public class RegexFileFilter implements FileFilter, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9212866553530195858L;
	private final Pattern pattern;

	/**
	 * @param pattern {@link Pattern}
	 */
	public RegexFileFilter(final Pattern pattern) {
		this.pattern = pattern;
	}

	/**
	 * @param regex regular expression
	 */
	public RegexFileFilter(final String regex) {
		this(Pattern.compile(regex));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.io.FileFilter#accept(java.io.File)
	 */
	public boolean accept(final File pathname) {
		return pattern.matcher(pathname.getName()).matches();
	}

}
